package com.acme.bankapp.server.command;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.acme.bankapp.domain.account.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.service.BankReport;

public class ReportCommand extends BankCommand {

	public ReportCommand(Bank bank, String arg) {
		super(bank, arg);
	}

	@Override
	public String execute() {
		StringBuilder sb = new StringBuilder();

		switch (arg) {
		case "nclients":
			return String.format("number of clients in bank: %d",
					BankReport.getNbOfClients(bank));
		case "naccounts":
			return String.format("total number of accounts in bank: %d",
					BankReport.getNbOfAccounts(bank));
		case "accountsbysum":
			sb.append("account by sum (descending):\n");
			for (Account account : BankReport.getAccountsSortedBySum(bank)) {
				sb.append(String.format("amount: %.2f\t%s\n",
						account.getMaximumAmountToWithdraw(), account));
			}
			return sb.toString();
		case "credit":
			return String.format("total bank credit: %.2f",
					BankReport.getBankCredit(bank));
		case "map":
			Map<Client, List<Account>> map = BankReport
					.getClientsAccountsMap(bank);
			sb.append("clients and their accounts:\n");
			for (Entry<Client, List<Account>> entry : map.entrySet()) {
				sb.append(entry.getKey());
				sb.append('\n');
				for (Account account : entry.getValue()) {
					sb.append(String.format("\t%s\n", account));
				}
			}
			return sb.toString();
		default:
			return "unknown report requested";
		}
	}

}
